<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\RegistrationLog;
use App\Models\CompanyModel;
use App\Models\JobPositionUpdateModel;
use App\Models\RegistrationLogModel;
use App\Services\Tenancy\Tenancy;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class RegistrationLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new RegistrationLog('company'), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('company.company_name', '公司')->display(function () {
                $value =$this->company;
                return $value->cur_type == CompanyModel::TYPE_PERSON ? $value->contact_person_name : $value->company_name;
            })->limit(10);
            $grid->column('change_num')->display(function ($value){
                if ($this->cur_type == RegistrationLogModel::CUR_TYPE_ADD){
                    $value =   "<span class='text-primary'>+{$value}</span>";
                }else{
                    $value = "<span class='text-danger'>-{$value}</span>";
                }
                return $value;
            });
            $grid->column('cur_type')->using(RegistrationLogModel::TYPE_LIST)->label([
                'add' => 'success',
                'cut' => 'danger',
            ]);
            $grid->column('remark');
            $grid->column('operate_at');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            if (request('company_id') >0){
                $grid->header(function () {
//                    $registration_num = auth('company')->user()->registration_num;
                    $registration_num = CompanyModel::query()->where('id',request('company_id'))->value('registration_num')??0;
                    // 自定义组件
                    $card = new Card();
                    $div = "<div style='width: 300px;margin: 5px 0;padding-left: 20px;background: #f6f6eb;color: #d6922f;border-radius:.25rem;height: 40px;line-height:40px'><i class='fa fa-exclamation-circle'></i> 当前还剩$registration_num 个报名单</div>";
                    return $card->content($div);
                });
            }
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('cur_type','变动类型')->select(RegistrationLogModel::TYPE_LIST)->width(2);
                $filter->equal('company_id', '公司')->select(Tenancy::selectLis())->width(2);
                $filter->between('operate_at', '操作时间')->datetime()->width(4);
            });
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            $grid->disableEditButton();
            //边框模式
//            $grid->withBorder();
            //横向滚动条
//            $grid->scrollbarX();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            $grid->disableActions();

            $grid->disableCreateButton();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new RegistrationLog(), function (Show $show) {
            $show->field('id');
            $show->field('company_id');
            $show->field('num');
            $show->field('before_num');
            $show->field('change_num');
            $show->field('after_num');
            $show->field('cur_type');
            $show->field('remark');
            $show->field('operate_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new RegistrationLog(), function (Form $form) {
            $form->display('id');
            $form->text('company_id');
            $form->text('num');
            $form->text('before_num');
            $form->text('change_num');
            $form->text('after_num');
            $form->text('cur_type');
            $form->text('remark');
            $form->text('operate_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
